package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentServiceImpl  implements CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 保存操作
     * 操作类型： 1-对动态点赞，2-对动态评论，3-对动态喜欢  4-对视频点赞   5-对视频评论    6-对评论点赞
     * 1.往操作表中添加一行记录
     * 2.修改动态详情表中记录数
     * @param comment
     * @return (返回操作之后的次数)
     */
    @Override
    public Integer saveMovementComment(Comment comment) {
        //2.1 查询到操作的哪个动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        //1.往操作表中添加一行记录(视频点赞,评论,喜欢 ,  动态的点赞,评论,喜欢)
        comment.setPublishUserId( movement.getUserId() ); //赋值动态详情所属人
        mongoTemplate.save(comment);

        //2.修改动态详情表中记录数
        //2.2 对哪个操作+1
        int count = 0;
        if(comment.getCommentType() == 1){ //动态点赞
            movement.setLikeCount(movement.getLikeCount() + 1);
            count = movement.getLikeCount();
        }else if(comment.getCommentType() == 2){//评论
            movement.setCommentCount( movement.getCommentCount() + 1 );
            count = movement.getCommentCount();
        }else if(comment.getCommentType() == 3){//喜欢
            movement.setLoveCount( movement.getLoveCount() + 1 );
            count = movement.getLoveCount();
        }
        //3.保存到mongo中
        mongoTemplate.save(movement); // 如果存在数据就是修改操作  如果是没有数据 执行添加操作

       return count;
    }

    /**
     * 删除操作
     * 操作类型： 1-对动态点赞，2-对动态评论，3-对动态喜欢  4-对视频点赞   5-对视频评论    6-对评论点赞
     * 1.往操作表中添加一行记录
     * 2.修改动态详情表中记录数
     * @param comment
     * @return
     */
    @Override
    public Integer deleteMovementComment(Comment comment) {
        //删除数据的时候 需要通过三个  谁对哪个动态进行的什么操作 进行删除
        Query query = new Query(
                Criteria.where("userId").is( comment.getUserId() )
                .and("publishId").is( comment.getPublishId() )
                //.and(comment.getCol()).is(comment.getCommentType())  这个字段没写对
                .and("commentType").is(comment.getCommentType())
        );
        //1.删除操作表一条数据
        mongoTemplate.remove( query , Comment.class);
        //2.数量-1
        //2.1 先查询出原来的对象
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //2.2 修改数量
        int count = 0;
        if(comment.getCommentType() == 1){ //动态点赞
            movement.setLikeCount(movement.getLikeCount() - 1);
            count = movement.getLikeCount();
        }else if(comment.getCommentType() == 2){//评论
            movement.setCommentCount( movement.getCommentCount() - 1 );
            count = movement.getCommentCount();
        }else if(comment.getCommentType() == 3){//喜欢
            movement.setLoveCount( movement.getLoveCount() - 1 );
            count = movement.getLoveCount();
        }
        //2.3 更新到数据库
        mongoTemplate.save(movement);

        return count;
    }

    /**
     * 根据动态的id查询动态的评论列表
     * @param page
     * @param pageSize
     * @param movementId
     * @return
     */
    @Override
    public PageBeanVo findCommentList(Integer page, Integer pageSize, String movementId) {
        int startIndex = (page-1) * pageSize;
        //封装条件
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId ))
                .and("commentType").is(2)//必须是评论才查询
        ).with(Sort.by(Sort.Order.desc("created")))
        .skip(startIndex).limit(pageSize);

        //根据动态的id查询动态的评论列表
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long counts = mongoTemplate.count(query, Comment.class);//总记录数
        return new PageBeanVo(page , pageSize , counts , commentList);
    }

    /**
     * 根据用户的id查询出状态信息
     * @param page
     * @param pageSize
     * @param commentType
     * @param loginUserId
     * @return
     */
    @Override
    public PageBeanVo findMessagesByCommentType(Integer page, Integer pageSize, int commentType, Long loginUserId) {
        int startIndex = (page -1 ) * pageSize;
        //根据用户的id和 查询的类型 查询出评论表对应的数据
        Query query = new Query(
                Criteria.where("commentType").is(commentType).and("publishUserId").is(loginUserId)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long counts = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(page , pageSize , counts ,commentList );
    }

    //保存视频的各种评论
    @Override
    public Integer saveVideoComment(Comment comment) {

        //根据评论查询视频详情
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);

        //保存评论到视频表
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(video.getUserId());
        mongoTemplate.save(comment);

        //视频里的点赞数、喜欢数、评论数增加1
        if(comment.getCommentType()==4){
            video.setLikeCount(video.getLikeCount()+1);
        }else if (comment.getCommentType()==5){
            video.setCommentCount(video.getCommentCount()+1);
        }

        //更新详情表
        mongoTemplate.save(video);


        //返回点赞数、评论数
        if(comment.getCommentType() == 4 ){
            return video.getLikeCount();
        }else if(comment.getCommentType() == 5 ){
            return video.getCommentCount();
        }
        return 0;

    }


    //删除视频的各种评论
    @Override
    public Integer deleteVideoComment(Comment comment) {

        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);


        //通过动态id、操作者id、评论类型删除评论
        Query query=new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );

        mongoTemplate.remove(query,Comment.class);

        if(comment.getCommentType() == 4 ){
            video.setLikeCount( video.getLikeCount() - 1 );
        }else if(comment.getCommentType() == 5 ){
            video.setCommentCount( video.getCommentCount() - 1 );
        }

        mongoTemplate.save(video);



        //返回点赞数、评论数
        if(comment.getCommentType() == 4 ){
            return video.getLikeCount();
        }else if(comment.getCommentType() == 5 ){
            return video.getCommentCount();
        }


        return 0;

    }

    //评论点赞
    @Override
    public Integer saveCommentLike(Comment comment) {

        //查询评论对象
        Comment parentComment = mongoTemplate.findById(comment.getPublishId(), Comment.class);

        //保存点赞
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(parentComment.getUserId());
        mongoTemplate.save(comment);

        //更改点赞数
        parentComment.setLikeCount(parentComment.getLikeCount()+1);

        mongoTemplate.save(parentComment);

        return parentComment.getLikeCount();

    }

    //评论取消点赞
    @Override
    public Integer deleteCommentLike(Comment comment) {

        //查询评论对象
        Comment parentComment = mongoTemplate.findById(comment.getPublishId(), Comment.class);

        //通过动态id、操作者id、评论类型删除评论
        Query query=new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );

        mongoTemplate.remove(query,Comment.class);

        parentComment.setLikeCount(parentComment.getLikeCount()-1);

        mongoTemplate.save(parentComment);

        return parentComment.getLikeCount();

    }



    //小视频评论列表查询
    @Override
    public PageBeanVo findCommentByVideoId(Integer page, Integer pageSize, String videoUserId) {

        int startIndex=(page-1)*pageSize;

        Query query=new Query(
                Criteria.where("publishId").is(new ObjectId(videoUserId)).and("commentType").is(5)
        ).with(Sort.by(Sort.Order.desc("created"))).skip(startIndex).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long counts = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(page,pageSize,counts,commentList);

    }
}
